//
//  HomeTableViewController.swift
//  Weibo
//
//  Created by 5201-mac on 2017/4/15.
//  Copyright © 2017年 wlb-mac. All rights reserved.
//

import UIKit

class HomeTableViewController: BaseTableViewController {
    
    lazy var statuses: [Status] = [Status]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        loadData()
    }
    
    func loadData() {
        guard let token = UserAccount.loadAccount()?.access_token else {
            return
        }
        print(token)
        let urlString = "https://api.weibo.com/2/statuses/home_timeline.json"
        let dict = ["access_token": token]
        let AFN = AFHTTPSessionManager()
        AFN.get(urlString, parameters: dict, progress: nil, success: { (_, responseObject) in
            //print(responseObject)
            //SVProgressHUD.showSuccess(withStatus: "微博发布成功")
            //解析数据，展示数据
            let data = responseObject as AnyObject
            let DataArray = data["statuses"]
            //print(DataArray)
            
            var list = [Status]()
            //遍历数组 字典转模型
            for (_,item) in (DataArray as! Array<AnyObject>).enumerated(){
                let s = Status(dict: item as! [String : NSObject])
                list.append(s)
            }
            
            //显示数据
            self.statuses = list
            //刷新列表
            self.tableView.reloadData()
        }) { (_, error) in
            print(error)
            SVProgressHUD.showError(withStatus: "发布失败")
        }
        
//        HTTPNetworkTools.sharedTools().requestJSONDict(.GET, url: urlString, parameters: dict) { (dict, error) in
//            if error != nil {
//                SVProgressHUD.showError(withStatus: ErrorTip)
//                return
//            }
//            //解析数据，展示数据
//            print(dict)
//            guard let array = dict!["statuses"] as? [[String: NSObject]] else {
//                SVProgressHUD.showError(withStatus: ErrorTip)
//                return
//            }
//            print(array)
//            var list = [Status]()
//            //遍历数组，字典转模型
//            for item in array {
//                let s = Status(dict: item)
//                list.append(s)
//            }
//            //显示数据
//            self.statuses = list
//            //刷新列表
//            self.tableView.reloadData()
//        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        
    }
    
    // MARK: - Table view data source
    
    override func numberOfSections(in tableView: UITableView) -> Int {
        
        return 1
    }
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        
        return statuses.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withIdentifier: "HomeCell") as! StatusCell

        //根据indexPath获取模型，给cell的模型赋值
        cell.status = statuses[indexPath.row]
        //cell!.backgroundColor = randomColor()
        
        return cell
    }
    
    
    /*
     // Override to support conditional editing of the table view.
     override func tableView(_ tableView: UITableView, canEditRowAt indexPath: IndexPath) -> Bool {
     // Return false if you do not want the specified item to be editable.
     return true
     }
     */
    
    /*
     // Override to support editing the table view.
     override func tableView(_ tableView: UITableView, commit editingStyle: UITableViewCellEditingStyle, forRowAt indexPath: IndexPath) {
     if editingStyle == .delete {
     // Delete the row from the data source
     tableView.deleteRows(at: [indexPath], with: .fade)
     } else if editingStyle == .insert {
     // Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view
     }
     }
     */
    
    /*
     // Override to support rearranging the table view.
     override func tableView(_ tableView: UITableView, moveRowAt fromIndexPath: IndexPath, to: IndexPath) {
     
     }
     */
    
    /*
     // Override to support conditional rearranging of the table view.
     override func tableView(_ tableView: UITableView, canMoveRowAt indexPath: IndexPath) -> Bool {
     // Return false if you do not want the item to be re-orderable.
     return true
     }
     */
    
    /*
     // MARK: - Navigation
     
     // In a storyboard-based application, you will often want to do a little preparation before navigation
     override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
     // Get the new view controller using segue.destinationViewController.
     // Pass the selected object to the new view controller.
     }
     */
    
}

